#!/bin/bash
#  收银宝交易流水电子小票
#  在 work 中处理 文件 ,   从 eticket 中获取源文件 ,  解析出来搬到一个 固定目录, 然后 入库 ,  入库完成删掉;
#  指定 日期( 20231102 )  分支( gx,  hn, gz )   环境(dev , prod )....
#  示例   bash    mariadb_load_pos_eticket.sh  -t  2023-04-10  -e  dev  -b gx

#读取小票文件...............................
readonly  BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
cd $BASE_DIR || exit;
echo "base_dir   ===>  $BASE_DIR  "
export TZ=Asia/Shanghai
shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] &&  alias date='gdate'

DATE=$(date -d "yesterday" +%Y-%m-%d)       # 除非指定,  否则默认今天
BRANCH="gx"                                 # 默认gx 这个分支
ENV="dev"                                   #默认测试环境
FORCE_IMPORT="NO"
FORCE_IMPORT_ARG=""
DRY_RUN_ARG=""



readonly DATA_DIR="/eticket-storage/data-files"
readonly DOWNLOAD_DIR="/eticket-storage/eticket-files"




while getopts "b:e:t:fD" opt; do
  case ${opt} in
    b )
      BRANCH="$OPTARG"  #  分支参数
      ;;
    e )
      ENV="$OPTARG"     #  环境参数
      ;;
    t )
      DATE="$OPTARG"    # 时间参数
      ;;
    f )
      FORCE_IMPORT="YES"
      FORCE_IMPORT_ARG="--force"
      ;;
    D )
      DRY_RUN_ARG="--dryRun"
      echo "DRYRUN: DRY_RUN_ARG=$DRY_RUN_ARG"
      ;;
    * )
      echo "Invalid option: $opt requires an argument" 1>&2
      ;;
  esac
done
shift $((OPTIND -1))

if [ "${BRANCH}" == "" ]; then
  echo "BRANCH is required. valid options are: gx, gz, hn  ;   If not ,   default  is   gx"
  BRANCH="gx"
fi

echo  "指定环境是$ENV "


if [[ "${ENV}" ==  "" ]] ; then
  echo "ENV   is required. valid options are: dev, prod ;   If not ,   default  is  dev "
  ENV="dev"
fi

CONFIG_FILE=$BASE_DIR/config/mysql_client_$ENV.conf


if [[ "${DATE}" ==  "" ]] ; then
  DATE=$(date -d "yesterday" +%Y-%m-%d)
fi
echo  " date  ==> ${DATE}  "
year_month=$(date -d "$DATE" +%Y%m)   #  yyyy-MM-dd  ==>  yyyymm

year=$(date -d "$DATE" +%Y)           #  yyyy-MM-dd  ==>  yy
month=$(date -d "$DATE" +%m)          #  yyyy-MM-dd  ==> mm
day=$(date -d "$DATE" +%d)            #  yyyy-MM-dd   ==> dd

cd    "$DATA_DIR/$BRANCH/$ENV/$year_month/$day"    #源文件路径
echo  "路径 ==>  $(pwd) "
trans_date="$year-$month-$day"
echo    "trans_date is  $trans_date"



#记录导入当天有多少条
count_sql="select count(id)   before_import_count  from    $database.t_vsp_pos_trans_img  where trans_date='$trans_date'"
mysql --defaults-file=$CONFIG_FILE -e "$count_sql"




#预处理 ， 防止重复， 先删掉 当天导入的；
delete_sql="delete from  $database.t_vsp_pos_trans_img  where trans_date='$trans_date'"
mysql --defaults-file=$CONFIG_FILE -e "$delete_sql"



# 对整个目录进行解析 ；
for   item  in    $(ls | grep "tar")     ;  do

  echo " 解析文件  $item "

  mkdir  -p  "${DOWNLOAD_DIR}/$BRANCH/$ENV/$year_month/$day"
  tar   xf   "$item"   -C  ${DOWNLOAD_DIR}"/$BRANCH/$ENV/$year_month/$day"
  cd   ${DOWNLOAD_DIR}"/$BRANCH/$ENV/$year_month/$day"



  vsp_txt=$(basename $item  .tar  ).txt
  echo  "txt file is  $vsp_txt  "
  echo  $(pwd)


  echo  "write  trans_date   to file ...... ,  vsp_txt is ==> "
  sed "s/^/${trans_date},&/g"   -i  $vsp_txt


  head -2   $vsp_txt        #参看头部两个数据



  eticket_dir="$BRANCH/$ENV/$year_month/$day"


  vsp_txt=$DOWNLOAD_DIR/$eticket_dir/$vsp_txt  #拼装好绝对路径；





  #将采集信息插入到数据库中
  loadCommand="load data local infile '$vsp_txt' into\
  table t_vsp_pos_trans_img  \
  fields terminated BY ',' lines terminated BY '\n' \
  (trans_date, merchant_no, terminal_no, card_pay_daytime, pay_no, trans_flow_no, ticket_no, amount, @eticket_file_field) \
  set eticket_file=concat('/$eticket_dir/', @eticket_file_field);"
  echo   -e "loadCommand==> \n $loadCommand"
  mysql --defaults-file=$CONFIG_FILE --local-infile=1 -e "$loadCommand"


  #统计导入条数
  echo   -e  " execute result is ==>  $?  \n导入的条数是 ==>   $( cat   $vsp_txt  | wc -l  )  "


  #  用完删除 txt 文件 ；
  rm  -f   $vsp_txt
  echo "$vsp_txt  已删除 "



done




#导入最后有多少条；
count_sql="select count(id)  after_import_count from $database.t_vsp_pos_trans_img  where trans_date='$trans_date'"
mysql --defaults-file=$CONFIG_FILE -e "$count_sql"


echo "导入数据到MariaDB完成, ENV=$ENV, BRANCH=$BRANCH, DATE=$DATE"






